function ready() {
    var MARK_TYPE = {
        "0": "other"
        ,"3": "singlemessage"
        ,"4": "groupmessage"
        ,"1": "timeline"
        ,"2": "mp"
    }

    clientCollect.getFromMark = function() {
        var q = getQuery();
        var from = q.from || q.pi_from_mark;
        if (!from) {
            var referrer = document.referrer;
            if (referrer.indexOf("mp.weixin.qq.com") !== -1 || referrer.indexOf("mp.weixinbridge.com") !== -1) {
                from = MARK_TYPE[2];
            } else {
                from = MARK_TYPE[0];
            }
        }
        return from || ''
    }

    clientCollect.execute();

    var pvSubject = clientCollect.collectSubjects.pageview;
    if (pvSubject) {
        var from_mark = clientCollect.getFromMark()
        var q = getQuery();
        pvSubject.set(
            "sharer_id"
            ,q.pi_sharer_id || ""
        );
        pvSubject.set(
            "from_mark"
            ,from_mark
        );
    };

    if (__PI_COLLECT_CONF.auto) {
        clientCollect.pv({
            "path": window.location.pathname
            ,"prev_page": document.referrer
            ,"current_page": window.location.pathname
        });
    }
}

function bindReady() {
    // 防止在绑定document的ready事件监听之前document已经触发ready事件
    // 直接执行
    if (doc.readyState === 'complete') {
        return defer(ready);
    }

    // firefox或其他标准浏览器支持此方法
    domEvent.on(doc, 'DOMContentLoaded', onDOMLoaded);

    // 保险方法：监听win的load事件，这个事件任何情况下都能正常工作
    domEvent.on(win, 'load', onDOMLoaded);
}

function onDOMLoaded() {
    // 移除各load监听器
    domEvent.off(doc, 'DOMContentLoaded', onDOMLoaded);
    domEvent.off(doc, 'readystatechange', onDOMLoaded);
    domEvent.off(win, 'load', onDOMLoaded);
    // 调用ready执行监听ready的函数
    setTimeout(function(){
        ready();
    },50);
}

bindReady();
